package cn.candy.pet.service.impl;

import cn.candy.basic.exception.BusinessExcpetion;
import cn.candy.basic.service.impl.BaseServiceImpl;
import cn.candy.basic.util.LoginContextUtil;
import cn.candy.org.domain.Employee;
import cn.candy.org.mapper.EmployeeMapper;
import cn.candy.pet.domain.Pet;
import cn.candy.pet.domain.PetDetail;
import cn.candy.pet.mapper.PetDetailMapper;
import cn.candy.pet.mapper.PetMapper;
import cn.candy.pet.service.IPetService;
import cn.candy.user.domain.Logininfo;
import cn.candy.user.domain.User;
import cn.candy.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 宠物业务实现类
 */
@Service
public class PetServiceImpl  extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Transactional
    public void add(Pet pet,HttpServletRequest request) {
        //获取管理员 - 设置shop_id
        Logininfo logininfo = LoginContextUtil.getLogininfo(request);
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        pet.setShop_id(employee.getShop_id());

        //添加宠物信息
        super.add(pet);
        //添加明细
        PetDetail detail = pet.getDetail();
        if(detail != null){
            detail.setPet_id(pet.getId());
            petDetailMapper.save(detail);
        }
    }

    @Override
    @Transactional
    public void del(Long id) { //传递的是宠物id
        //删除宠物的信息
        super.del(id);
        //删除宠物详情
        petDetailMapper.removeByPetId(id);
    }


    @Override
    @Transactional
    public void batchRemove(List<Long> ids) {
        //删除宠物的信息
        super.batchRemove(ids);
        //删除宠物详情
        petDetailMapper.batchRemoveByPetIds(ids);
    }


    @Override
    @Transactional
    public void update(Pet pet) {
        //修改宠物的信息
        super.update(pet);
        //修改宠物详情
        PetDetail detail = pet.getDetail();
        if(detail!= null){
            detail.setPet_id(pet.getId());
            petDetailMapper.updateByPetId(detail);
        }
    }

    @Override
    public void offsale(List<Long> ids) {
        //Mybatis多参数操作:1.下标 - 不建议 2.注解 - 建议 3.Map
        Map<String, Object> map = new HashMap<>();
        map.put("offsaletime",new Date());
        map.put("ids",ids);
        petMapper.offSale(map);
    }

    @Override
    public void onsale(List<Long> ids) {
        //Mybatis多参数操作:1.下标 - 不建议 2.注解 - 建议 3.Map
        Map<String, Object> map = new HashMap<>();
        map.put("onsaletime",new Date());
        map.put("ids",ids);
        petMapper.onsale(map);
    }

    @Override
    public void adopt(Long id, HttpServletRequest request) {
        Pet pet = petMapper.loadById(id);
        //1.下架
        pet.setState(0);
        //2.设置下架时间
        pet.setOffsaletime(new Date());
        //3.绑定用户id
        Logininfo logininfo = LoginContextUtil.getLogininfo(request);
        User user = userMapper.findByLogininfoId(logininfo.getId());
        pet.setUser_id(user.getId());
        pet.setUser(user);
        //修改新的状态
        petMapper.update(pet);
        //4.生成订单 @TODO
        //5.支付 @TODO
    }

    @Override
    public void isShopAdmin(HttpServletRequest request) {
        //获取管理员
        Logininfo logininfo = LoginContextUtil.getLogininfo(request);
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        //什么管理员
        if(employee.getShop_id() == null){//平台管理员
            throw new BusinessExcpetion("平台管理员");
        }
    }

    /*@Override
    public Pet findOne(Long id) {
        Pet pet = petMapper.loadById(id);
        PetDetail petDetail = petDetailMapper.loadByPetId(id);
        pet.setDetail(petDetail);
        return pet;
    }*/
}
